/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MyClass;

import java.util.Random;
import javax.microedition.lcdui.Image;

/**
 *
 * @author HuuTai_it
 */
   
public class Monster extends Ship {
    public Monster (GameManagement gameMng, Image image) throws Exception {
        
        super(gameMng, image);
        
        boolean temp = true;
        while(temp) {
            
            int x = Math.abs(gameMng.r.nextInt() % 10);
            int y = Math.abs(gameMng.r.nextInt() % 10);
            int mX = gameMng.mainShip.getX() / 24;
            int mY = gameMng.mainShip.getY() / 24;
            
            if(gameMng.element[x][y] == 0 && (Math.abs(x - mX) > 3 || Math.abs(y - mY) > 3)) {
                
                setPosition(x * 24, y * 24);
                gameMng.element[x][y] = 4;
                temp = false;
            }
        }
    }
    public int autoMove(Ship s)
    {
        int[] arr = {0, 0, 0, 0, 0, 0, 0, 0};
        Random r = new Random();
        if( !startMove && count == 0)
        {
            if(!startMove)
            {
                if(getX() < 24 && ( direction == 6 ||direction == 7|| direction == 8) ) 
                direction = 3;
                else if(getX() >= 216 && ( direction == 2 ||direction == 3|| direction == 4) ) 
                    direction = 7;
                if(getY() < 24 && ( direction == 1 ||direction == 2|| direction == 8) ) 
                    direction = 5;
                else if(getY() >= 216 && ( direction == 4 ||direction == 5|| direction == 6) ) 
                    direction = 1;
            }
            boolean f = true;
            while((gameMng.element[getX()/24 + dirc[0][direction - 1]/24]
                    [getY()/24+ dirc[1][direction - 1]/24] != 0  
                   ||  gameMng.element[getX()/24 + dirc[0][direction - 1]/24]
                   [getY()/24+ dirc[1][direction - 1]/24] == 1 ||
                    gameMng.element[getX()/24 + dirc[0][direction - 1]/24]
                    [getY()/24+ dirc[1][direction - 1]/24] == 9 || f  ) &&
                            arr[0]*arr[1]*arr[2]*arr[3]*arr[4]*arr[5]*arr[6]*arr[7] == 0)
                    {
                        f = false;
                        do
                        {
                            direction =  Math.abs(r.nextInt() % 8) + 1;
                        }
                        while(arr[direction - 1] > 0  && (arr[0]*arr[1]*arr[2]*arr[3]*arr[4]*arr[5]*arr[6]*arr[7] == 0));
                        arr[direction - 1]++;
                        if(getX() < 24 && ( direction == 6 ||direction == 7|| direction == 8) ) 
                            direction = 3;
                        else if(getX() >= 216 && ( direction == 2 ||direction == 3|| direction == 4) ) 
                            direction = 7;
                        if(getY() < 24 && ( direction == 1 ||direction == 2|| direction == 8) ) 
                            direction = 5;
                        else if(getY() >= 216 && ( direction == 4 ||direction == 5|| direction == 6) ) 
                            direction = 1;
                    }
                //tim dc huong di
            gameMng.element[getX()/24][getY()/24] = 0;
            gameMng.element[getX()/24 + dirc[0][direction - 1]/24]
                    [getY()/24 + dirc[1][direction - 1]/24] = 4;
            startMove = true;
            count++;
            xDes = getX() + dirc[0][direction - 1];
            yDes = getY() + dirc[1][direction - 1];
            return 0;
        }
        if(getX() == xDes && getY() == yDes)
        {
            startMove = false;   
            count = 0;
            return 1;//endmove
        }
        if(startMove)       
        {            
            move(dirc[0][direction - 1]/12, dirc[1][direction - 1]/12);
            switch(direction) {            
            case 1:setFrameSequence(frameUp);break;
            case 2:setFrameSequence(frameUpRight);break;
            case 3:setFrameSequence(frameRight);break;
            case 4:setFrameSequence(frameDownRight);break;
            case 5:setFrameSequence(frameDown);break;
            case 6:setFrameSequence(frameDownLeft);break;
            case 7:setFrameSequence(frameLeft);break;
            case 8:setFrameSequence(frameUpLeft);break;
            } 
        }
        return 2;
    }
    public int fire(){
        return 0;
    }
}
